<template>
	<view class="withdraw-container page-container">
		<!-- 当前余额显示 -->
		<view class="balance-info">
			<view class="balance-card">
				<text class="balance-label">可转账余额</text>
				<text class="balance-amount">¥ {{ availableBalance }}</text>
				<text class="balance-tip">待结算：¥ {{ frozenBalance }}</text>
			</view>
		</view>

		<!-- 转账表单 -->
		<view class="withdraw-form">
			<!-- 转账金额 -->
			<view class="form-section">
				<view class="section-title">
					<text class="title-text">转账金额</text>
				</view>

				<view class="amount-input-container">
					<view class="currency-symbol">¥</view>
					<u-input v-model="transferAmount" type="number" placeholder="请输入转账金额" :border="false"
						:clearable="true" fontSize="16" class="amount-input" @input="onAmountInput"></u-input>
				</view>

				<view class="amount-actions">
					<button class="all-btn" @click="transferAll">全部转账</button>
				</view>
			</view>

			<!-- 转账对象 -->
			<view class="form-section">
				<view class="section-title">
					<text class="title-text">对方账号</text>
				</view>

				<u-form class="alipay-info">
					<u-form-item label="手机" :border-bottom="true">
						<u-input v-model="toUserInfo.mobile" placeholder="请输入对方的手机号" maxlength="11"
							@input="loadToUserInfo"></u-input>
					</u-form-item>
					<u-form-item label="姓名" :border-bottom="false">
						<u-input v-model="toUserInfo.realname" disabled></u-input>
					</u-form-item>
					<u-form-item label="昵称" :border-bottom="false">
						<u-input v-model="toUserInfo.nickname" disabled></u-input>
					</u-form-item>
				</u-form>
			</view>

			<!-- 转账说明 -->
			<view class="withdraw-tips">
				<view class="tips-title">
					<u-icon name="info-circle" size="16" color="#999"></u-icon>
					<text class="tips-text">转账说明</text>
				</view>
				<view class="tips-content">
					<text class="tip-item">• 转账金额最低20元，单日最高转账5000元</text>

				</view>
			</view>
		</view>

		<!-- 底部转账按钮 -->
		<view class="bottom-action">
			<view class="fee-info">
				<text class="actual-text">实际到账：¥ {{ transferAmount }}</text>
			</view>
			<button class="withdraw-btn" :class="{ disabled: !canTransfer }" :disabled="!canTransfer"
				@click="handleTransfer">
				确认转账
			</button>
		</view>

		<!-- 支付密码弹窗 -->
		<u-popup :show="passwordPopupShow" mode="center" border-radius="20" @close="passwordPopupShow = false">
			<view class="password-popup">
				<view class="popup-header">
					<text class="popup-title">输入支付密码</text>
					<u-icon name="close" size="20" color="#999" @click="passwordPopupShow = false"></u-icon>
				</view>
				<view class="popup-content">
					<text class="popup-amount">¥ {{ transferAmount }}</text>
					<view class="password-input">
						<view class="password-dot" v-for="n in 6" :key="n"
							:class="{ filled: n < paymentPassword.length }"></view>
					</view>
				</view>
				<view class="number-keyboard">
					<view class="keyboard-row" v-for="(row, rowIndex) in keyboard" :key="rowIndex">
						<view class="keyboard-key" v-for="(key, keyIndex) in row" :key="keyIndex"
							@click="inputPassword(key)">
							<text class="key-text" v-if="key !== 'delete'">{{ key }}</text>
							<u-icon name="backspace" size="20" color="#333" v-else></u-icon>
						</view>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex'
	import {
		validatePhone
	} from '@/common/validate.js'

	export default {
		data() {
			return {
				orderid: '',
				// 可转账余额
				availableBalance: '0.00',
				// 冻结余额
				frozenBalance: '0.00',
				// 转账金额
				transferAmount: '',
				// 转账对象信息
				toUserInfo: {
					id: '0',
					mobile: '',
					realname: '',
					nickname: ''
				},
				passwordPopupShow: false, // 支付密码弹框
				paymentPassword: '', // 支付密码
				keyboard: [
					['1', '2', '3'],
					['4', '5', '6'],
					['7', '8', '9'],
					['', '0', 'delete']
				]
			}
		},
		computed: {
			...mapGetters(['isLogin', 'userInfo']),
			// 是否可以转账
			canTransfer() {
				const amount = parseFloat(this.transferAmount)
				const available = parseFloat(this.availableBalance)

				// if (amount < 20 || amount > 5000 || amount > available || Number(this.toUserInfo.id) <= 0) {
				// 	return false
				// }
				if (amount < 0 || amount > 5000 || amount > available || Number(this.toUserInfo.id) <= 0) {
					return false
				}

				return true
			}
		},
		onShow() {
			this.loadAccountInfo();
		},
		methods: {
			// 查询账户信息
			loadAccountInfo() {
				this.$api.user.GetAccountInfo().then(res => {
					if (res.data && res.data.balance) {
						this.availableBalance = res.data.balance
						this.frozenBalance = res.data.transit_commission
					}
				})
			},

			// 获取用户信息 by 手机号
			loadToUserInfo() {
				if (!validatePhone(this.toUserInfo.mobile)) {
					return
				}
				if (this.toUserInfo.mobile === this.$secret.decryptAES(this.userInfo.mobile)) {
					uni.showToast({
						title: '不允许转账给自己',
						icon: 'none'
					})
					return
				}
				this.$api.user.GetUserInfoByMobile({
					mobile: this.toUserInfo.mobile
				}).then(res => {
					this.toUserInfo.id = res.data.id
					this.toUserInfo.realname = res.data.realname
					this.toUserInfo.nickname = res.data.nickname
				})
			},

			// 金额输入处理
			onAmountInput(value) {
				// 限制小数点后两位
				if (value.includes('.')) {
					const parts = value.split('.')
					if (parts[1] && parts[1].length > 2) {
						this.transferAmount = parts[0] + '.' + parts[1].substring(0, 2)
					}
				}
			},

			// 全部转账
			transferAll() {
				this.transferAmount = this.availableBalance
			},

			// 处理转账
			handleTransfer() {
				if (!this.canTransfer) {
					return
				}

				// 是否设置了支付密码
				if (this.userInfo.has_password_secure !== '已设置') {
					uni.showModal({
						title: '支付密码',
						content: '您尚未设置支付密码，是否立即设置？',
						success: (res) => {
							if (res.confirm) {
								uni.redirectTo({
									url: '/pages/user/pay-password'
								})
							}
						}
					})
					return
				}

				const amount = parseFloat(this.transferAmount)

				// 显示确认弹窗				
				uni.showModal({
					title: '确认转账',
					content: `转账金额：¥${amount} \u000a实际到账：¥${amount}`,
					success: (res) => {
						if (res.confirm) {
							this.$api.user.AddTransferOrders({
								from_user_id: this.userInfo.id,
								to_user_id: this.toUserInfo.id,
								transfer_amount: amount,
								remark: ''
							}).then(res => {
								this.orderid = res.data
								this.passwordPopupShow = true
							})
						}
					}
				})
			},

			// 输入支付密码
			inputPassword(key) {
				if (key === 'delete') {
					this.paymentPassword = this.paymentPassword.slice(0, -1)
				} else if (key && this.paymentPassword.length < 6) {
					this.paymentPassword += key

					// 密码输入完成
					if (this.paymentPassword.length === 6) {
						setTimeout(() => {
							this.processTransfer()
						}, 300)
					}
				}
			},

			// 处理转账流程
			processTransfer() {
				// 显示加载提示
				uni.showLoading({
					title: '处理中...'
				})

				this.$api.user.TransferForUpdateAccountInfo({
					id: this.orderid,
					paymentPassword: this.$secret.encryptAES(this.paymentPassword), // 支付密码(加密)
				}).then(res => {
					uni.hideLoading();
					uni.showToast({
						title: res.msg,
						icon: 'success',
						duration: 2000
					})

					setTimeout(() => {
						uni.navigateBack()
					}, 2000)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.withdraw-container {
		background-color: #f5f5f5;
		min-height: 100vh;
		padding: 20rpx;
		padding-bottom: calc(160rpx + env(safe-area-inset-bottom));
	}

	.balance-info {
		margin-bottom: 30rpx;
	}

	.balance-card {
		background: linear-gradient(135deg, #52a58a 0%, #459a7e 100%);
		border-radius: 16rpx;
		padding: 30rpx;
		text-align: center;
		color: white;
	}

	.balance-label {
		font-size: 26rpx;
		opacity: 0.9;
		display: block;
		margin-bottom: 10rpx;
	}

	.balance-amount {
		font-size: 48rpx;
		font-weight: 600;
		display: block;
		margin-bottom: 10rpx;
	}

	.balance-tip {
		font-size: 24rpx;
		opacity: 0.8;
	}

	.withdraw-form {
		background-color: #ffffff;
		border-radius: 16rpx;
		padding: 30rpx;
		margin-bottom: 30rpx;
	}

	.form-section {
		margin-bottom: 40rpx;
	}

	.form-section:last-child {
		margin-bottom: 0;
	}

	.section-title {
		margin-bottom: 20rpx;
	}

	.title-text {
		font-size: 32rpx;
		font-weight: 600;
		color: #333;
	}

	.amount-input-container {
		display: flex;
		align-items: center;
		background-color: #f8f9fa;
		border-radius: 12rpx;
		padding: 20rpx;
		border: 2rpx solid #e5e5e5;
		margin-bottom: 20rpx;
	}

	.currency-symbol {
		font-size: 32rpx;
		color: #333;
		margin-right: 10rpx;
		font-weight: 600;
	}

	.amount-input {
		flex: 1;
	}

	.amount-actions {
		text-align: right;
	}

	.all-btn {
		background-color: transparent;
		color: #52a58a;
		border: 1rpx solid #52a58a;
		border-radius: 20rpx;
		padding: 10rpx 20rpx;
		font-size: 24rpx;
	}

	.withdraw-methods {
		border-radius: 12rpx;
		overflow: hidden;
		border: 1rpx solid #e5e5e5;
	}

	.method-item {
		display: flex;
		align-items: center;
		padding: 30rpx 20rpx;
		background-color: #ffffff;
		border-bottom: 1rpx solid #f0f0f0;
		transition: all 0.3s ease;
	}

	.method-item:last-child {
		border-bottom: none;
	}

	.method-item.active {
		background-color: #f8fffe;
		border-color: #52a58a;
	}

	.method-icon {
		margin-right: 20rpx;
	}

	.method-info {
		flex: 1;
	}

	.method-name {
		font-size: 28rpx;
		color: #333;
		font-weight: 500;
		display: block;
		margin-bottom: 5rpx;
	}

	.method-desc {
		font-size: 24rpx;
		color: #999;
	}

	.alipay-info,
	.bank-info {
		border-radius: 12rpx;
		overflow: hidden;
		border: 1rpx solid #e5e5e5;
	}

	.withdraw-tips {
		background-color: #f8f9fa;
		border-radius: 12rpx;
		padding: 20rpx;
	}

	.tips-title {
		display: flex;
		align-items: center;
		gap: 8rpx;
		margin-bottom: 15rpx;
	}

	.tips-text {
		font-size: 26rpx;
		color: #666;
		font-weight: 500;
	}

	.tips-content {
		padding-left: 24rpx;
	}

	.tip-item {
		display: block;
		font-size: 24rpx;
		color: #999;
		line-height: 1.6;
		margin-bottom: 8rpx;
	}

	.bottom-action {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		padding: 20rpx;
		padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
		background-color: #ffffff;
		border-top: 1rpx solid #e5e5e5;
	}

	.fee-info {
		display: flex;
		justify-content: space-between;
		margin-bottom: 15rpx;
		padding: 0 10rpx;
	}

	.fee-text,
	.actual-text {
		font-size: 24rpx;
		color: #666;
	}

	.actual-text {
		color: #52a58a;
		font-weight: 600;
	}

	.withdraw-btn {
		width: 100%;
		height: 88rpx;
		background-color: #52a58a;
		color: white;
		border: none;
		border-radius: 12rpx;
		font-size: 32rpx;
		font-weight: 600;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.withdraw-btn.disabled {
		background-color: #cccccc;
		color: #999999;
	}

	/*支付密码框*/

	.password-popup {
		width: 600rpx;
		background-color: #ffffff;
		border-radius: 20rpx;
		overflow: hidden;
	}

	.popup-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 40rpx;
		border-bottom: 2rpx solid #f5f5f5;
	}

	.popup-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
	}

	.popup-content {
		padding: 40rpx;
		text-align: center;
	}

	.popup-amount {
		font-size: 48rpx;
		font-weight: bold;
		color: #ff4757;
		margin-bottom: 40rpx;
	}

	.password-input {
		display: flex;
		justify-content: center;
		margin-bottom: 40rpx;
	}

	.password-dot {
		width: 40rpx;
		height: 40rpx;
		border: 2rpx solid #e5e5e5;
		border-radius: 50%;
		margin: 0 10rpx;
	}

	.password-dot.filled {
		background-color: #333;
	}

	.number-keyboard {
		border-top: 2rpx solid #f5f5f5;
	}

	.keyboard-row {
		display: flex;
	}

	.keyboard-key {
		flex: 1;
		height: 100rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		border-right: 2rpx solid #f5f5f5;
		border-bottom: 2rpx solid #f5f5f5;
	}

	.keyboard-key:last-child {
		border-right: none;
	}

	.keyboard-row:last-child .keyboard-key {
		border-bottom: none;
	}

	.key-text {
		font-size: 36rpx;
		color: #333;
	}
</style>